package com.lp.common.poster.core.decorators;

import com.lp.common.poster.core.abst.AbstractPosterDecorator;
import com.lp.common.poster.core.abst.Poster;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.awt.*;
import java.awt.image.BufferedImage;

import static com.lp.common.constant.CommonConstant.AUTHOR;

/**
 * @program: biz
 * @description:
 * @author: Ke.Song
 * @since: 2024-04-03 21:52:23
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class TextDecorator extends AbstractPosterDecorator {

    /**
     * 字体
     */
    private Font font = new Font(null);

    /**
     * 字体样式
     */
    private int fontStyle = Font.PLAIN;

    /**
     * 字体大小
     */
    private int fontSize = 16;

    /**
     * 字体颜色
     */
    private Color color = new Color(0, 0, 0);

    /**
     * 内容
     */
    private String content;

    /**
     * 是否包含删除线
     */
    private boolean delLine = false;

    /**
     * 是否根据 width 换行, 配合 width 使用
     */
    private boolean canNewLine = false;

    /**
     * 换行限制次数
     */
    private int newLineLimit = 0;

    /**
     * 行距（默认1.5，表示1.5倍行距）
     */
    private double lineHeight = 1.5;

    private String type;

    /**
     * 是否带省略号，默认带
     */
    boolean ellipsis;


    public TextDecorator(Poster poster) {
        super(poster);
    }

    @Builder(toBuilder = true)
    public TextDecorator(Poster poster, int positionX, int positionY, int width, int height, Font font, int fontSize, Color color, String content, int fontStyle, boolean delLine, boolean canNewLine, int newLineLimit, double lineHeight, String type, boolean ellipsis) {
        super(poster, positionX, positionY, width, height);
        this.font = font;
        this.fontSize = fontSize;
        this.color = color;
        this.content = content;
        this.fontStyle = fontStyle;
        this.delLine = delLine;
        this.canNewLine = canNewLine;
        this.newLineLimit = newLineLimit;
        this.lineHeight = lineHeight;
        this.type = type;
        this.ellipsis = ellipsis;
    }

    @Override
    public BufferedImage draw(BufferedImage image) {
        // 绘制 被装饰之前的 图片
        BufferedImage draw = poster.draw(image);
        // 装饰, 绘制文本
        return drawText(draw);
    }

    /**
     * 绘制文本具体实现
     *
     * @param image image
     * @return image
     */
    private BufferedImage drawText(BufferedImage image) {

        if (content == null || content.isEmpty()) {
            return image;
        }

        // 实现绘制文本
        font = font.deriveFont(fontStyle, fontSize);
        Graphics2D g = image.createGraphics();
        // 设置文字抗锯齿算法
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setFont(font);
        g.setColor(color);

        FontMetrics fm = g.getFontMetrics(font);

        int charWidth = 0;
        StringBuilder sb = new StringBuilder();
        if (AUTHOR.equals(type)) {
            for (int i = 0; i < content.length(); i++) {
                charWidth += fm.charWidth(content.charAt(i));
                sb.append(content.charAt(i));
                if (charWidth >= width * (newLineLimit + 1)) {
                    content = sb.delete(sb.length() - 2, sb.length()).append(ellipsis ? "..." : "").toString();
                    break;
                }
            }
            while (true) {
                if (positionX + charWidth > width) {
                    positionX -= (positionX + charWidth - width);
                } else if (positionX + charWidth < width) {
                    positionX += (width - positionX - charWidth);
                } else {
                    positionX -= 5;
                    break;
                }
            }
        }
        // 换行 并且 设置的换行宽度大于一个字的大小才可以换行
        if (canNewLine && width > fontSize) {
            //【商品名称换行判断逻辑】
            // 换行修正最后一个文字越界.
            width = width - fontSize/2;

            charWidth = 0;
            int lineLimit = newLineLimit;
            sb = new StringBuilder();

            for (int i = 0; lineLimit >= 0 && i < content.length(); i++) {
                charWidth += fm.charWidth(content.charAt(i));
                sb.append(content.charAt(i));
                // 如果宽度 > width 换行
                if (charWidth > width * (newLineLimit-lineLimit+1)) {
                    //增加1.5倍行距
                    g.drawString(sb.toString(), positionX, positionY + fontSize + (int)((newLineLimit-lineLimit) * fontSize *lineHeight));
                    sb.setLength(0);
                    lineLimit--;
                    continue;
                }
                // 如果超过 换行次数限制 则后面省略 改为...
                if (charWidth > width * (newLineLimit + 1) - 2 * fontSize) {
                    sb.append(ellipsis ? "..." : "");
                    //增加1.5倍行距
                    g.drawString(sb.toString(), positionX, positionY + fontSize + (int)((newLineLimit-lineLimit) * fontSize * lineHeight));
                    break;
                }
                // 如果前两个if没有进，则直接添加
                if (i == content.length() - 1) {
                    //增加1.5倍行距
                    g.drawString(sb.toString(), positionX, positionY + fontSize + (int)(((newLineLimit-lineLimit)) * fontSize * lineHeight));
                }
            }

        } else {
            g.drawString(content, positionX, positionY + fontSize);
        }

        // 删除线暂时不支持换行
        if (delLine && !canNewLine) {
            // 删除线长度
            int delLineW = fm.stringWidth(content);
            g.drawLine(positionX, positionY + 3 * fontSize / 5, positionX + delLineW, positionY + 3 * fontSize / 5);
        }

        g.dispose();
        return image;
    }

}

